# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_pyname=Pygments
_realname=pygments
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.2.0
pkgrel=2
pkgdesc="Python syntax highlighter (mingw-w64)"
arch=('any')
license=('BSD')
url="http://pygments.org/"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools" "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
_dtoken="71/2a/2e4e77803a8bd6408a2903340ac498cb0a2181811af7c9ec92cb70b0308a"
source=("https://pypi.python.org/packages/${_dtoken}/${_pyname}-${pkgver}.tar.gz")
noextract=("${_pyname}-${pkgver}.tar.gz")
sha256sums=('dbae1046def0efb574852fab9e90209b23f556367b5a320c0bcb871c77c3e8cc')

prepare() {
  # workaround for symlink issue
  tar -xf ${_pyname}-${pkgver}.tar.gz || true
  cp ${_pyname}-${pkgver}/scripts/debug_lexer.py ${_pyname}-${pkgver}/scripts/find_error.py
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf $builddir | true
    cp -r ${_pyname}-${pkgver} $builddir
  done

  cd python2-build-${CARCH}
  sed -e "s|#![ ]*/usr/bin/python$|#!/usr/bin/python2|" \
      -e "s|#![ ]*/usr/bin/env python$|#!/usr/bin/env python2|" \
      -e "s|#![ ]*/bin/env python$|#!/usr/bin/env python2|" \
      -i $(find . -name '*.py')

}

check() {
  for pver in {2,3}; do
    msg "Python ${pver} test for ${CARCH}"
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py check
  done
}

package_python3-pygments() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
  
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_f}
  done
}

package_python2-pygments() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools")

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_f}
  done

  for f in pygmentize; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe.manifest
      sed -e "s|${f}|${f}2|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}2.exe.manifest
    fi
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}-script.py
  done

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"
}

package_mingw-w64-i686-python2-pygments() {
  install=${_realname}2-${CARCH}.install
  package_python2-pygments
}

package_mingw-w64-i686-python3-pygments() {
  install=${_realname}3-${CARCH}.install
  package_python3-pygments
}

package_mingw-w64-x86_64-python2-pygments() {
  install=${_realname}2-${CARCH}.install
  package_python2-pygments
}

package_mingw-w64-x86_64-python3-pygments() {
  install=${_realname}3-${CARCH}.install
  package_python3-pygments
}
